package org.jeecg.modules.yks.srv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsService;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsMaterialDetail;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsProductDetail;
import org.jeecg.modules.yks.srv.entity.YksEquipmentDevopsStatus;
import org.jeecg.modules.yks.srv.mapper.YksEquipmentDevopsMaterialDetailMapper;
import org.jeecg.modules.yks.srv.mapper.YksEquipmentDevopsProductDetailMapper;
import org.jeecg.modules.yks.srv.mapper.YksEquipmentDevopsStatusMapper;
import org.jeecg.modules.yks.srv.mapper.YksEquipmentDevopsServiceMapper;
import org.jeecg.modules.yks.srv.service.IYksEquipmentDevopsServiceService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 运维主表
 * @Author: jeecg-boot
 * @Date:   2024-01-16
 * @Version: V1.0
 */
@Service
public class YksEquipmentDevopsServiceServiceImpl extends ServiceImpl<YksEquipmentDevopsServiceMapper, YksEquipmentDevopsService> implements IYksEquipmentDevopsServiceService {

	@Autowired
	private YksEquipmentDevopsServiceMapper yksEquipmentDevopsServiceMapper;
	@Autowired
	private YksEquipmentDevopsMaterialDetailMapper yksEquipmentDevopsMaterialDetailMapper;
	@Autowired
	private YksEquipmentDevopsProductDetailMapper yksEquipmentDevopsProductDetailMapper;
	@Autowired
	private YksEquipmentDevopsStatusMapper yksEquipmentDevopsStatusMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(YksEquipmentDevopsService yksEquipmentDevopsService, List<YksEquipmentDevopsMaterialDetail> yksEquipmentDevopsMaterialDetailList,List<YksEquipmentDevopsProductDetail> yksEquipmentDevopsProductDetailList,List<YksEquipmentDevopsStatus> yksEquipmentDevopsStatusList) {
		yksEquipmentDevopsServiceMapper.insert(yksEquipmentDevopsService);
		if(yksEquipmentDevopsMaterialDetailList!=null && yksEquipmentDevopsMaterialDetailList.size()>0) {
			for(YksEquipmentDevopsMaterialDetail entity:yksEquipmentDevopsMaterialDetailList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				entity.setEquipmentId(yksEquipmentDevopsService.getEquipmentId());
				yksEquipmentDevopsMaterialDetailMapper.insert(entity);
			}
		}
		if(yksEquipmentDevopsProductDetailList!=null && yksEquipmentDevopsProductDetailList.size()>0) {
			for(YksEquipmentDevopsProductDetail entity:yksEquipmentDevopsProductDetailList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				entity.setEquipmentId(yksEquipmentDevopsService.getEquipmentId());
				yksEquipmentDevopsProductDetailMapper.insert(entity);
			}
		}
		if(yksEquipmentDevopsStatusList!=null && yksEquipmentDevopsStatusList.size()>0) {
			for(YksEquipmentDevopsStatus entity:yksEquipmentDevopsStatusList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				yksEquipmentDevopsStatusMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(YksEquipmentDevopsService yksEquipmentDevopsService,List<YksEquipmentDevopsMaterialDetail> yksEquipmentDevopsMaterialDetailList,List<YksEquipmentDevopsProductDetail> yksEquipmentDevopsProductDetailList,List<YksEquipmentDevopsStatus> yksEquipmentDevopsStatusList) {
		yksEquipmentDevopsServiceMapper.updateById(yksEquipmentDevopsService);
		
		//1.先删除子表数据
		yksEquipmentDevopsMaterialDetailMapper.deleteByMainId(yksEquipmentDevopsService.getId());
		yksEquipmentDevopsProductDetailMapper.deleteByMainId(yksEquipmentDevopsService.getId());
		yksEquipmentDevopsStatusMapper.deleteByMainId(yksEquipmentDevopsService.getId());
		
		//2.子表数据重新插入
		if(yksEquipmentDevopsMaterialDetailList!=null && yksEquipmentDevopsMaterialDetailList.size()>0) {
			for(YksEquipmentDevopsMaterialDetail entity:yksEquipmentDevopsMaterialDetailList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				yksEquipmentDevopsMaterialDetailMapper.insert(entity);
			}
		}
		if(yksEquipmentDevopsProductDetailList!=null && yksEquipmentDevopsProductDetailList.size()>0) {
			for(YksEquipmentDevopsProductDetail entity:yksEquipmentDevopsProductDetailList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				yksEquipmentDevopsProductDetailMapper.insert(entity);
			}
		}
		if(yksEquipmentDevopsStatusList!=null && yksEquipmentDevopsStatusList.size()>0) {
			for(YksEquipmentDevopsStatus entity:yksEquipmentDevopsStatusList) {
				//外键设置
				entity.setDevopsId(yksEquipmentDevopsService.getId());
				yksEquipmentDevopsStatusMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void confirmStatus(Collection<? extends Serializable> idList, String status, String remark) {
//		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		for (Serializable id : idList) {
			YksEquipmentDevopsService service = yksEquipmentDevopsServiceMapper.selectById(id);
			if (service == null) {
				continue;
			}
			service.setStatus(status);
			yksEquipmentDevopsServiceMapper.updateById(service);

			YksEquipmentDevopsStatus entity = new YksEquipmentDevopsStatus();
			entity.setDevopsId(service.getId());
			entity.setStatus(status);
			entity.setRemark(remark);
			yksEquipmentDevopsStatusMapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		yksEquipmentDevopsMaterialDetailMapper.deleteByMainId(id);
		yksEquipmentDevopsProductDetailMapper.deleteByMainId(id);
		yksEquipmentDevopsStatusMapper.deleteByMainId(id);
		yksEquipmentDevopsServiceMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			yksEquipmentDevopsMaterialDetailMapper.deleteByMainId(id.toString());
			yksEquipmentDevopsProductDetailMapper.deleteByMainId(id.toString());
			yksEquipmentDevopsStatusMapper.deleteByMainId(id.toString());
			yksEquipmentDevopsServiceMapper.deleteById(id);
		}
	}
	
}
